package com.fix.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fix.dao.AuthDao;
import com.fix.dao.UserDao;
import com.fix.dao.impl.AuthDaoImpl;
import com.fix.dao.impl.UserDaoImpl;
import com.fix.entity.Authority;
import com.fix.entity.User;
import com.google.gson.Gson;

/**
 *  根据用户ID得到他的权利
 *  ajax请求  返回 JSON
 */
@WebServlet("/getauth")
public class GetUserAuthServlet extends HttpServlet {
	
	UserDao userDao =new UserDaoImpl();
	AuthDao authDao = new AuthDaoImpl();
	Gson gson = new Gson();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("userid");  //得到用户ID
		int userid = Integer.parseInt(id);
		List auths =null;
		try {
			auths = authDao.getAuthByUserid(userid);  //用户的权利
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 如何返回JSON数据
		String json = gson.toJson(auths);
		
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		response.setContentType("text/json;charset=UTF-8");
		out.print(json);
		out.flush();
		out.close();
		 
	}

	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
